package user.management;

import entity.Users;
import facade.UsersFacadeLocal;
import java.io.IOException;
import java.security.Principal;
import javax.ejb.EJB;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
 *
 * @author Tatyana
 */
public class LoginServlet1 extends HttpServlet {

    @EJB
    private UsersFacadeLocal usersFacade;

    public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request, response);
    }

    public void doPost(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException {

        RequestDispatcher rd;
        Principal currentUserPrincipal = req.getUserPrincipal();

//                Users user;
//		if (currentUserPrincipal != null) {
//                    
//                    name = req.getRemoteUser();
//                    user = usersFacade.readByLogin(name); 
//                   
//                    if(user==null){
//                    HttpSession session = req.getSession();
//                    session.setAttribute("user", user);
//			
//		}
        
        /*********************/
        
        String name = req.getRemoteUser();
        Users user = usersFacade.readByLogin(name);
        if (user != null) {
            HttpSession session = req.getSession();
            session.setAttribute("user", user);

        }
        rd = req.getRequestDispatcher("/main.jsp");
        rd.forward(req, res);
    }
}
